import { DialogService, TableWidthConfig } from 'ng-devui';
import { ReviewRolesService } from './../../../@core/services/ReviewRoles/review-roles.service';
import { Component } from '@angular/core';
import { AddReviewFlowRolesComponent } from './add-review-flow-roles/add-review-flow-roles.component';
import { EditReviewFlowRolesComponent } from './edit-review-flow-roles/edit-review-flow-roles.component';
import { QRCodeConfigAddComponent } from '../QRCodeConfig/qrcode-config-add/qrcode-config-add.component';

@Component({
  selector: 'app-review-flow-roles',
  templateUrl: './review-flow-roles.component.html',
  styleUrls: ['./review-flow-roles.component.scss']
})
export class ReviewFlowRolesComponent {
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any; }[];
  ReviewRoles:any;//审批角色
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '组名称',
        fieldType: 'text'
      },
      {
        field: 'type',
        header: '组成员',
        fieldType: 'text'
      }
    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: '操作',
      width: '150px'
    },
    {
      field:'name',
      width:''
    },
    {
      field: 'type',
      width: ''
    }
  ]
  config = {
    id: 'Add-Dirctoy-Type',
    maxHeight: '400px',
    backdropCloseable: true,
  }//弹窗配置
  constructor( private ReviewRolesService:ReviewRolesService,private dialogService: DialogService) { }
  ngOnInit(): void {
    this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.pager.total=data.totalCount;
      this.ReviewRoles=data.approvalGroups;
    })
  }
  pageIndexChange($event: any) {
    this.pager.pageIndex=$event;
    this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.ReviewRoles=data.approvalGroups;
    })//获取编码规则
  }//切换页码
  pageSizeChange($event: any){
    this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.ReviewRoles=data.approvalGroups;
    })//获取编码规则
  }//改变每页显示数量
  delete(id:any,dialogtype?:string){
    const results = this.dialogService.open({
      ...this.config,
      width: '400px',
      maxHeight: '600px',
      title: '删除审核组',
      content: '确认删除该审核组？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.ReviewRolesService.DeleteApprovalGroup(id).subscribe((data:any)=>{
              this.showToast('success','成功','审核组删除成功')
              results.modalInstance.hide();
              this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                this.ReviewRoles=data.approvalGroups;
              })//获取编码规则
            },(error: any) => {
              if (error.status === 404) {
                this.showToast('error', '错误', '网络连接错误');
              } else {
                this.showToast('error', '错误', error.error);
              }
            })
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//删除
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  AddReviewRoles(){
    let data={
      warning:false,
    }
    const results = this.dialogService.open({
      ...this.config,
      maxHeight: '600px',
      title: '新增审核组',
      data: data,
      content: AddReviewFlowRolesComponent,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {

            if(results.modalContentInstance.CreateApprovalGroup.name&&results.modalContentInstance.CreateApprovalGroup.userAccountNumber.length>0){
              this.ReviewRolesService.CreateApprovalGroup(results.modalContentInstance.CreateApprovalGroup).subscribe((data:any)=>{
                this.showToast('success','成功','审核组新增成功')
                results.modalInstance.hide();
                this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.ReviewRoles=data.approvalGroups;
                }
                )//获取编码规则
              },(error: any) => {
                if (error.status === 404) {
                  this.showToast('error', '错误', '网络连接错误');
                } else {
                  this.showToast('error', '错误', error.error);
                }
              })
            }else if(!results.modalContentInstance.CreateApprovalGroup.name&&results.modalContentInstance.CreateApprovalGroup.userAccountNumber.length>0){
              data.warning=true;
              this.showToast('error','失败','审核组名称不能为空')

            }else if(results.modalContentInstance.CreateApprovalGroup.name&&results.modalContentInstance.CreateApprovalGroup.userAccountNumber.length==0){
              data.warning=true;
              this.showToast('error','失败','审核组成员不能为空')
            }else{
              data.warning=true;
              this.showToast('error','失败','审核组名称和审核组成员不能为空')
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
            this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
              this.ReviewRoles=data.approvalGroups;
            })//获取编码规则
          },
        },
      ],
    });
  }//新增
  EditReviewRoles(rowItem:any){
    let data={
      warning:false,
      rowItem:rowItem
    }
    const results = this.dialogService.open({
      ...this.config,
      maxHeight: '600px',
      title: '更新审核组',
      data: data,
      content: EditReviewFlowRolesComponent,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.UpdateApprovalGroup.name&&results.modalContentInstance.UpdateApprovalGroup.groups.length>0){
              this.ReviewRolesService.UpdateApprovalGroup(rowItem.approvalGroupId,results.modalContentInstance.UpdateApprovalGroup).subscribe((data:any)=>{
                this.showToast('success','成功','审核组编辑成功')
                results.modalInstance.hide();
                this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.ReviewRoles=data.approvalGroups;
                }
                )//获取编码规则
              },(error: any) => {
                if (error.status === 404) {
                  this.showToast('error', '错误', '网络连接错误');
                } else {
                  this.showToast('error', '错误', error.error);
                }
              })
            }else if(!results.modalContentInstance.UpdateApprovalGroup.name&&results.modalContentInstance.UpdateApprovalGroup.groups.length>0){
              data.warning=true;
              this.showToast('error','失败','审核组名称不能为空')

            }else if(results.modalContentInstance.UpdateApprovalGroup.name&&results.modalContentInstance.UpdateApprovalGroup.groups.length==0){
              data.warning=true;
              this.showToast('error','失败','审核组成员不能为空')
            }else{
              data.warning=true;
              this.showToast('error','失败','审核组名称和审核组成员不能为空')
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
            this.ReviewRolesService.GetPagedApprovalGroup(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
              this.ReviewRoles=data.approvalGroups;
            })//获取编码规则
          },
        },
      ],
    });
  }//编辑


}
